import axios from 'axios';

import Config from '../../config';
import Web3Manager from '../../libs/web3-manager';
import AuthenticationManager from '../authentication/authentication-manager';
import getAPIHeaders from '../get-api-headers';
import {
    Action,
    Organization,
    OrganizationActionResponse,
    OrganizationResponse
} from './organization';

export default class OrganizationManager {
    static organization: Organization = null;

    static getProfile = async (): Promise<Organization> => {
        if (OrganizationManager.organization != null) {
            return OrganizationManager.organization;
        } else {
            if ((await Web3Manager.getWalletAddress()) != null) {
                const call = await axios.get(Config.getAPI() + '/organization/profile', {
                    headers: {
                        [AuthenticationManager.KEY]: AuthenticationManager.getKey(),
                        [AuthenticationManager.SECRET]: AuthenticationManager.getSecret(),
                        wallet_address: await Web3Manager.getWalletAddress()
                    }
                });

                if (call.data['organization']) {
                    const organization = new Organization();

                    organization.id = call.data['organization']['id'];
                    organization.name = call.data['organization']['name'];
                    organization.walletAddress = call.data['organization']['wallet_address'];

                    OrganizationManager.organization = organization;
                    return organization;
                }
            }

            return null;
        }
    };

    static getAllOrganization = async (
        status: number,
        page: number = 0,
        limit: number = 20,
        sort_by: string = '',
        order_by: string = 'asc',
        search: string = ''
    ): Promise<OrganizationResponse> => {
        try {
            const res = await axios.get<OrganizationResponse>(
                Config.getAPI() +
                    `/pending-organization/all?status=${status}&page=${page}&limit=${limit}&sort_by=${sort_by}&order_by=${order_by}&search=${search}`,
                {
                    headers: await getAPIHeaders()
                }
            );
            return res.data;
        } catch (error) {}
    };

    static actionOrganization = async (
        action: Action,
        organization_id: number
    ): Promise<OrganizationActionResponse> => {
        try {
            switch (action) {
                case Action.Approved:
                    const resApproved = await axios.post<OrganizationActionResponse>(
                        Config.getAPI() +
                            `/pending-organization/approve?pending_organization_id=${organization_id}`,
                        {},
                        {
                            headers: await getAPIHeaders()
                        }
                    );
                    return resApproved.data;
                default:
                    const resDenied = await axios.post<OrganizationActionResponse>(
                        Config.getAPI() +
                            `/pending-organization/deny?pending_organization_id=${organization_id}`,
                        {},
                        {
                            headers: await getAPIHeaders()
                        }
                    );
                    return resDenied.data;
            }
        } catch (error) {
            const empty: OrganizationActionResponse = {
                pendingOrganzizations: null
            };
            return empty;
        }
    };
}
